package com.snoob.springCloudGateway.rateLimiter.filter;

import com.snoob.springCloudGateway.rateLimiter.filter.keyResolver.HostAddressKeyResolver;
import com.snoob.springCloudGateway.rateLimiter.filter.keyResolver.UrlKeyResolver;
import com.snoob.springCloudGateway.rateLimiter.filter.keyResolver.UserKeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

/**
 * @描述: 基于SpringCloudGate网关的Filer过滤器对请求进行限流
 * @作者: lixing
 * @日期 2021/7/17 14:20
 */
@Component
@Configuration
public class RequestRateLimiterByGatewayFilterConfig {
    /**
     * @描述 获取请求用户ip作为限流key
     * @作者 lixing
     * @日期 2021/7/17 14:21
     * @Param []
     * @return org.springframework.cloud.gateway.filter.ratelimit.KeyResolver
     *
     * 配置方法：参考网关过滤器的 RequestRateLimiter 配置项
     */
    @Bean
    public HostAddressKeyResolver hostAddressKeyResolver() {
        return new HostAddressKeyResolver();
    }

    /**
     * @描述 获取请求用户作为限流key
     * @作者 lixing
     * @日期 2021/7/17 14:22
     * @Param []
     * @return org.springframework.cloud.gateway.filter.ratelimit.KeyResolver
     *
     * 配置方法：参考网关过滤器的 RequestRateLimiter 配置项
     */
    @Bean
    public UserKeyResolver userKeyResolver() {
        return new UserKeyResolver();
    }

    /**
     * @描述 获取请求地址的URL地址作为限流key
     * @作者 lixing
     * @日期 2021/7/17 14:26
     * @Param [] 
     * @return org.springframework.cloud.gateway.filter.ratelimit.KeyResolver
     *
     * 配置方法：参考网关过滤器的 RequestRateLimiter 配置项
     */
    @Bean
    @Primary
    public UrlKeyResolver urlKeyResolver() {
        return new UrlKeyResolver();
    }
}